import { _decorator, Component, Sprite, SpriteAtlas, resources, Input } from 'cc';
import { Card } from './Card';
import DataManager from './global/DataManager';
import { ICard } from './common';
import EventManager from './manages/EventManager';
import { EventEnum } from './Enum';
import { Seat } from './Seat';
const { ccclass, property } = _decorator;

@ccclass('CardTemplate')
export class CardTemplate extends Component {
    id: number;

    @property
    type: String; //wan tong tiao feng zhongfabai hongheihua

    @property
    index: number;

    @property
    state: number = 0; // 0 牌堆 1 手牌 2 出过的牌 3 吃牌/碰/杠牌/花牌 

    directionInFrist: number; // 在第一视角下的方位

    // private 

    isSelect: boolean = false;
    start() {

    }
    onLoad() {

        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        EventManager.Instance.on(EventEnum.Discard, this.DiscardHandle, this)
    }

    DiscardHandle(cardTemplate: CardTemplate) {
        // const mySeat = DataManager.Instance.mm.fristSeat
        const mySeat = DataManager.Instance.mm.node.children.find(e => {
            // console.log(e);

            if (e.getComponent(Seat) && e.getComponent(Seat).id == DataManager.Instance.mySeatId) {
                return e;
            }

            //   return  e.id == DataManager.Instance.mm
        })
        
        if (this.isSelect && mySeat.getComponent(Seat).isMyTurn) {
            cardTemplate.node.setParent(mySeat.getChildByName("action"))
            // DataManager.Instance.mm.
            EventManager.Instance.emit(EventEnum.TurnEnd)


        }

    }
    onTouchStart(e) {
        if (this.isSelect) {

            EventManager.Instance.emit(EventEnum.Discard, this)
            return
        }

        if (this.directionInFrist == 0 && this.state == 1) {
            const parent = this.node.getParent()
            parent.children.forEach(item => {
                const card = item.getComponent(CardTemplate)
                console.log(item);
                if (card.id !== this.id) {
                    card.isSelect = false
                }
            })
            this.isSelect = true
        }

    }



    onDestroy() {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
        EventManager.Instance.off(EventEnum.Discard, this.DiscardHandle, this)
    }

    public init(data: ICard) {
        this.id = data.id;
        this.type = data.type
        this.index = data.index
        this.state = data.state


        this.setCardSpriteFrame()
    }

    // 设置牌的精灵属性
    setCardSpriteFrame() {


        const sprite = this.node.getComponent(Sprite)


        const blueBasePath1 = "/majiang/blue1/";
        const blueBasePath2 = "/majiang/blue2/";
        let realPath = ""
        if (this.state == 0 || this.state == 1) {
            if (this.state == 1 && this.directionInFrist == DataManager.Instance.myDirction) {
                realPath = blueBasePath2 + this.type
                // console.log(this,"spriteAtlas");
                resources.load(realPath, SpriteAtlas, (err, spriteAtlas) => {


                    sprite.spriteAtlas = spriteAtlas;
                    sprite.spriteFrame = spriteAtlas.spriteFrames[this.index]

                })

            } else {

                realPath = blueBasePath1 + "back"
                resources.load(realPath, SpriteAtlas, (err, spriteAtlas) => {

                    sprite.spriteAtlas = spriteAtlas;
                    sprite.spriteFrame = spriteAtlas.spriteFrames[1]

                })
            }



        } else if (this.state == 2 || this.state == 3) {
            realPath = blueBasePath1 + this.type
            resources.load(realPath, SpriteAtlas, (err, spriteAtlas) => {
                sprite.spriteAtlas = spriteAtlas;
                sprite.spriteFrame = this.state == 0 ? spriteAtlas.spriteFrames[1] : spriteAtlas.spriteFrames[this.index]


            })
        }

    };
    /**
     * 
     * @param direction  self  after  face  font
     */
    updateCardSpriteFrame() {
   

        this.setCardSpriteFrame()

    }

    update(deltaTime: number) {
        if (this.state == 1 && this.directionInFrist == 0) {
            this.node.setPosition(this.node.position.x, this.isSelect ? 5 : 0, this.node.position.z)
        }

    }
}


